<template>
    <el-dialog v-model="visible" title="移除任务" :close-on-click-modal="false" :close-on-press-escape="false"
        v-loading="state.dataForm.loading" style="width: 30%;">
        <el-form :model="state.dataForm" ref="dataFormRef" label-width="85px" @keyup.enter="state.getDataList()">
            <el-form-item label="联络任务">
                <el-select v-model="state.dataForm.tskId" placeholder="请选择" style="width: 60%;">
                    <el-option v-for="role in roleList" :key="role.id" :label="role.tskName"
                        :value="role.id"></el-option>
                </el-select>
                <el-checkbox v-model="state.dataForm.contacted" style="margin-left: 18px;">已联络的也移除</el-checkbox>
            </el-form-item>
        </el-form>
        <template v-slot:footer>
            <div style="display: flex; justify-content: center;">
                <el-button type="danger" @click="gatdelete()">移除</el-button>
                <el-button @click="close()">关闭</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { reactive, ref, toRefs, } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const view = reactive({
    dataForm: {
        loading: false,
        id: "",
        tskId: "",
        name: "",
        checkAll: false as any,
        condition: "",
        contacted: false as any,

    },
    dataForm2: {
        psnIdList: [] as any[],
    },

});

const init = (id: any, data: any) => {
    console.log(data);
    visible.value = true;
    state.dataForm = data;
    state.dataForm.id = '';
    state.dataForm.checkAll = data.checkAll;
    state.dataForm2.psnIdList = data.psnIdList.split(',').map((x: string) => parseInt(x))
    // 重置表单数据
    if (dataFormRef.value) {
        dataFormRef.value.resetFields();
    }
    getTasklist()
};

//关闭
const close = () => {
    visible.value = false;
    state.dataForm.tskId = "";
}

//任务列表
const roleList = ref<any[]>([]);
const getTasklist = () => {
    return baseService.get("crm/crmtask/pageByUserId", { page: 1, limit: 500, orderBy: 'desc',isNeedExhId: '1'  }).then((res) => {
        roleList.value = res.data.list;
    });
};


// //移除
const gatdelete = () => {
    if (!state.dataForm.tskId) {
        ElMessage.warning('请选择联络任务');
        return;
    }
    state.dataForm.loading = true;
    state.dataForm2.psnIdList = state.dataForm2.psnIdList
    baseService.delete("/crm/crmlinkrecord/removeLinkTask", { tskId: Number(state.dataForm.tskId), psnIdList: state.dataForm2.psnIdList, checkAll: state.dataForm.checkAll, condition: state.dataForm, contacted: state.dataForm.contacted }).then((res) => {
        if (res.code == 0) {
            // 重新获取数据列表
            state.dataForm2.psnIdList = [];
            state.dataForm.tskId = "";
            state.dataForm.checkAll = false;
            state.dataForm.contacted = false;
            visible.value = false;
            state.dataForm.loading = false;
            state.multiple = !state.dataForm.checkAll;
            emit("refreshDataList");
            return ElMessage.success({
                message: "已将"+res.data.insertCount+"条数据从任务中移除",
                type: "success",
                duration: 5000,
            });
        }
    });
}

const state = reactive({ ...useView(view), ...toRefs(view) });

defineExpose({
    init
});
</script>
